//
//  PopUpView.m
//  小宝爱学
//
//  Created by imac on 15/9/9.
//  Copyright (c) 2015年 imac. All rights reserved.
//

#import "PopUpView.h"
#import "CustomDefine.h"
#import "Utils.h"


@implementation PopUpView
-(instancetype)initWithtext:(NSString*)text andView:(UIView*)view{
    CGSize size=[Utils getSizeByText:text andFontSize:17 andMaxSize:CGSizeMake(__kWidth, 30)];
    
    self=[super initWithFrame:CGRectMake(CGRectW(view)/2-(size.width+20)/2, CGRectH(view)-30-30, size.width+20, 30)];
    if (self) {
        self.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.layer.cornerRadius=5;
        self.layer.masksToBounds=YES;
        
        UILabel *textLb=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, size.width, 20)];
        textLb.text=text;
        textLb.font=MFont(17);
        textLb.textAlignment=NSTextAlignmentCenter;
        textLb.textColor=[UIColor whiteColor];
        [self addSubview:textLb];
        
        [view addSubview:self];
    }
    return self;
}
-(void)hiddenByDuration:(CGFloat)duration{
    if (duration<=0) {
        duration=2;
    }
    [UIView animateWithDuration:duration animations:^{
        self.alpha=0;
    }];
    [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(remove:) userInfo:self repeats:NO];
}
-(void)remove:(NSTimer*)timer{
    PopUpView *popUpView=(PopUpView*)timer.userInfo;
    [popUpView removeFromSuperview];
    [timer invalidate];
}
-(void)hidden{
    self.alpha=0;
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
